/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

/**
 *
 * @author user
 */
public class PublicKeyEncryption 
{
    PublicKey readKeyFromFile(String keyFileName) throws IOException {
 // InputStream in =  ServerConnection.class.getResourceAsStream(keyFileName);
  ObjectInputStream oin =null;
 //   new ObjectInputStream(new BufferedInputStream(in));
  try {
    BigInteger m = (BigInteger) oin.readObject();
    BigInteger e = (BigInteger) oin.readObject();
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PublicKey pubKey = fact.generatePublic(keySpec);
    return pubKey;
  } catch (Exception e) {
    throw new RuntimeException("Spurious serialisation error", e);
  } finally {
    oin.close();
  }
}
//    public byte[] rsaEncrypt(byte[] data) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
//  PublicKey pubKey = readKeyFromFile("/public.key");
//  Cipher cipher = Cipher.getInstance("RSA");
//  cipher.init(Cipher.ENCRYPT_MODE, pubKey);
// // byte[] cipherData = cipher.doFinal(src);
//  return cipherData;
//}
    
}
